﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.Services.Framework;
using GestionPedago.Services.ES.Modele;

namespace GestionPedago.Services.Mapping
{
    /// <summary>
    /// Classe de mapping Entity<->ES de l'objet Promotion
    /// </summary>
    public class PromotionMappeur : IMappeur<Promotion, PromotionES>
    {
        /// <summary>
        /// Constructeur
        /// </summary>
        public PromotionMappeur() { }

        /// <summary>
        /// Mapping Entity -> ES
        /// </summary>
        /// <param name="entity">L'entity à mapper</param>
        /// <returns>L'ES obtenu</returns>
        public PromotionES Entity2ES(Promotion entity)
        {
            PromotionES retour = new PromotionES();
            retour.CodePromotion = entity.CodePromotion;
            retour.Libelle = entity.Libelle;
            retour.Debut = entity.Debut;
            retour.Fin = entity.Fin;
            retour.CodeFormation = entity.CodeFormation;
            retour.PrixPublicAffecte = entity.PrixPublicAffecte;
            retour.DateModif = entity.DateModif;
            retour.DateCreation = entity.DateCreation;
            retour.PrixPECAffecte = entity.PrixPECAffecte;
            retour.PrixFinanceAffecte = entity.PrixFinanceAffecte;
            retour.ListeCours = new List<CoursES>();
            foreach (Cours c in entity.Cours)
            {
                retour.ListeCours.Add(new CoursMappeur().Entity2ES(c));
            }
            retour.Formation = new FormationMappeur().Entity2ES(entity.Formation);
            //TODO implémenter mapping Entity<->ES manuellement ! : PlanningIndividuelFormation

            retour.ListePlanningECF = new List<PlanningECFES>();
            /////foreach(PlanningECF r in entity.PlanningECF) {
            //    // Mapping du stagiaire Entity vers l'ES et ajout dans la liste
            //  //  PlanningECFES pee = new PlanningECFMappeur().Entity2ES(r);
            //    pee.DateSuggeree = pee.DateECF;
            //    pee.DejaPlanifie = true;
            //    retour.ListePlanningECF.Add(pee);
            //}
            //foreach(ECF e in entity.GetListeECFAPasser()) {
            //    PlanningECFES pee = new PlanningECFES();
            //    pee.ECF = new ECFMappeur().Entity2ES(e);
            //    pee.Promotion = retour;
            //    pee.DateSuggeree = e.GetSuggestionDate(entity);
            //    pee.DejaPlanifie = false;
            //    retour.ListePlanningECF.Add(pee);
            //}

            return retour;
        }

        /// <summary>
        /// Mapping ES -> Entity
        /// </summary>
        /// <param name="es">L'ES à mapper</param>
        /// <returns>L'entity obtenu</returns>
        public Promotion ES2Entity(PromotionES es)
        {
            Promotion retour = new Promotion();
            retour.CodePromotion = es.CodePromotion;
            retour.Libelle = es.Libelle;
            retour.Debut = es.Debut;
            retour.Fin = es.Fin;
            retour.CodeFormation = es.CodeFormation;
            retour.PrixPublicAffecte = es.PrixPublicAffecte;
            retour.DateModif = es.DateModif;
            retour.DateCreation = es.DateCreation;
            retour.PrixPECAffecte = es.PrixPECAffecte;
            retour.PrixFinanceAffecte = es.PrixFinanceAffecte;
            foreach (CoursES c in es.ListeCours)
            {
                retour.Cours.Add(new CoursMappeur().ES2Entity(c));
            }
            //retour.Formation = new FormationMappeur().ES2Entity(es.Formation);
            //TODO implémenter mapping Entity<->ES manuellement ! : PlanningIndividuelFormation
            //foreach(PlanningECFES p in es.ListePlanningECF) {
            //    if(p.DejaPlanifie) {
            //        retour.PlanningECF.Add(new PlanningECFMappeur().ES2Entity(p));
            //    }
            //}
            if (!EntityStateChecker.IsDetached(retour))
            {
                EntityConnection.GetContext().Promotion.ApplyCurrentValues(retour);
            }
            return retour;
        }
    }
}
